# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=virtualpg
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase=mingw-w64-${_realname}
pkgver=2.0.1
pkgrel=1
pkgdesc="A loadable dynamic extension module to both SQLite and SpatiaLite implementing VirtualPostgres (direct SQL cross access to PostgreSQL/PostGIS tables) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gaia-gis.it/fossil/virtualpg/index"
license=('GPLv2')
depends=("${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-postgis")
options=('strip' 'staticlibs')
source=("https://www.gaia-gis.it/gaia-sins/${_realname}-$pkgver.tar.gz")
sha256sums=('be2aebeb8c9ff274382085f51d422e823858bca4f6bc2fa909816464c6a1e08b')

prepare() {
  cd ${_realname}-${pkgver}
  autoreconf -fiv
}


build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"

  cd "${srcdir}/build-${MINGW_CHOST}"
	../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=mingw32 \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}

	make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  make DESTDIR=${pkgdir} install

  # Fix mod_virtualpg location to be possible: SELECT load_extension('mod_virtualpg');
  mv "${pkgdir}${MINGW_PREFIX}/lib/mod_virtualpg.dll" "${pkgdir}${MINGW_PREFIX}/bin/"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
